/*
 * Copyright 2004-2005 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）* 复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.projectname.init;

import org.apache.log4j.Logger;

import com.companyname.common.base.util.PriUtils;
import com.companyname.common.basedata.define.TypeParams;
import com.companyname.common.basedata.manager.TypeManager;
import com.companyname.common.basedata.model.Type;
import com.companyname.common.sysframe.manager.OrgManager;
import com.companyname.common.sysframe.manager.PostManager;
import com.companyname.common.sysframe.manager.RoleManager;
import com.companyname.common.sysframe.manager.UserManager;
import com.companyname.common.sysframe.model.Org;
import com.companyname.common.sysframe.model.Post;
import com.companyname.common.sysframe.model.Role;
import com.companyname.common.sysframe.model.User;
import com.companyname.common.viewframe.define.ViewFrameParams;
import com.companyname.common.viewframe.manager.AreaManager;
import com.companyname.common.viewframe.manager.ColumnManager;
import com.companyname.common.viewframe.manager.HomeManager;
import com.companyname.common.viewframe.model.Area;
import com.companyname.common.viewframe.model.Column;
import com.companyname.common.viewframe.model.Home;
import com.companyname.projectname.define.ProjectNameTypeFinalParams;


/**
 * 利用hibernate初始化数据库数据
 * 
 * @author amonlei
 *
 */
public class InitData
{
        static Logger logger = Logger.getLogger(InitData.class);
        
        private OrgManager orgManager;
        
        private RoleManager roleManager;
        
        private PostManager postManager;
        
        private UserManager userManager;
        
        private HomeManager homeManager;
        
        private AreaManager areaManager;
        
        private ColumnManager columnManager;
        
        private TypeManager typeManager;
        
        private int index;

        /**
         * @param orgManager 要设置的 orgManager。
         */
        public void setOrgManager(OrgManager orgManager)
        {
                this.orgManager = orgManager;
        }

        /**
         * @param postManager 要设置的 postManager。
         */
        public void setPostManager(PostManager postManager)
        {
                this.postManager = postManager;
        }

        /**
         * @param roleManager 要设置的 roleManager。
         */
        public void setRoleManager(RoleManager roleManager)
        {
                this.roleManager = roleManager;
        }
        
        /**
         * @param userManager 要设置的 userManager。
         */
        public void setUserManager(UserManager userManager)
        {
                this.userManager = userManager;
        }
        
        /**
         * @param areaManager 要设置的 areaManager。
         */
        public void setAreaManager(AreaManager areaManager)
        {
                this.areaManager = areaManager;
        }

        /**
         * @param columnManager 要设置的 columnManager。
         */
        public void setColumnManager(ColumnManager columnManager)
        {
                this.columnManager = columnManager;
        }

        /**
         * @param homeManager 要设置的 homeManager。
         */
        public void setHomeManager(HomeManager homeManager)
        {
                this.homeManager = homeManager;
        }
        
        /**
         * @param typeManager 要设置的 typeManager
         */
        public void setTypeManager(TypeManager typeManager)
        {
                this.typeManager = typeManager;
        }

        // 初始化db
        public void init()
        {
               logger.info("初始化数据库数据");
               if(this.userManager.getByAccount("admin") == null)
               {
                       User admin = this.initSys(1);
                       this.initBaseData(admin);
                       logger.debug("初始化完毕");
               }
               else
               {
                       logger.debug("已经初始化过");
               }
        }
        
        // 优先级
        public String getPri()
        {
                return PriUtils.value(this.index ++);
        }
        
        // 停止
        public void stop()
        {
                logger.info("初始化完毕");
        }
        
        // 系统管理初始化
        public User initSys(int index)
        {
                logger.info(index + ".1. 初始化sysframe的数据");
                // 机构
                logger.debug("初始化机构");
                Org rootOrg = new Org("companyname",
                                      "公司名",
                                      this.getPri());
                this.orgManager.save(rootOrg);
                logger.debug("保存" + rootOrg.getName());
                
                // 角色
                logger.debug("初始化角色");
                Role userRole = new Role("user","用户",this.getPri());
                this.roleManager.save(userRole);
                logger.debug("保存" + userRole.getName());
                Role adminRole = new Role("admin","管理员",this.getPri());
                adminRole.setSuperRole(userRole);
                this.roleManager.save(adminRole);
                logger.debug("保存" + adminRole.getName());
                
                // 岗位
                logger.debug("初始化岗位");
                Post companyname_user_post = new Post(rootOrg,userRole);
                this.postManager.save(companyname_user_post);
                logger.debug("保存" + companyname_user_post.getName());
                Post companyname_admin_post = new Post(rootOrg,adminRole);
                this.postManager.save(companyname_admin_post);
                logger.debug("保存" + companyname_admin_post.getName());
                
                // 人员
                logger.debug("初始化人员");
                User admin = new User("admin",
                                      "管理",
                                      "员",
                                      this.getPri());
                admin.addPost(companyname_admin_post,0);
                this.userManager.save(admin);
                logger.debug("保存" + admin.getName());
                
                logger.info(index + ".2. 初始化viewframe的数据");
                // 主页
                logger.debug("初始化主页");
                Home defaultHome = new Home(ViewFrameParams.DEFAULT_HOME,
                                     "默认",
                                     this.getPri());
                this.homeManager.save(defaultHome);
                
                // 分区
                logger.debug("初始化分区");
                Area funcColumnsArea = new Area(defaultHome,
                                         ViewFrameParams.DEFAULT_HOME_FUNC_AREA,
                                         "功能栏",
                                         this.getPri());
                this.areaManager.save(funcColumnsArea);
                
                // 栏目
                logger.debug("初始化栏目");
                Column basedataColumn = new Column(funcColumnsArea,
                                                   "",
                                                   "基础数据",
                                                   "",
                                                   this.getPri());
                this.columnManager.save(basedataColumn);
                  
                          Column typeColumn = new Column(basedataColumn,
                                                         "",
                                                         "类型",
                                                         "column_141_img.gif",
                                                         "moduleinfor/basedata/type/xtreeroot.jsp",
                                                         this.getPri());
                          this.columnManager.save(typeColumn);                
                Column moduleNameColumn = new Column(funcColumnsArea,
                                                    "",
                                                    "模块名",
                                                    "",
                                                    this.getPri());
                   this.columnManager.save(moduleNameColumn);
                   
                           Column modelNameColumn = new Column(moduleNameColumn,
                                                              "",
                                                              "实体名",
                                                              "GongWuSQ_01.gif",
                                                              "moduleinfor/modulename/modelname/xtreeroot.jsp",
                                                              this.getPri());
                           this.columnManager.save(modelNameColumn);
                           
                Column personColumn = new Column(funcColumnsArea,
                                                 "",
                                                 "个人事务",
                                                 "",
                                                 this.getPri());
                this.columnManager.save(personColumn);
                
                        Column changepwColumn = new Column(personColumn,
                                                           "",
                                                           "修改密码",
                                                           "GongWuSQ_01.gif",
                                                           "moduleinfor/changepw/root.jsp",
                                                           this.getPri());
                        this.columnManager.save(changepwColumn);
                
                Column logColumn = new Column(funcColumnsArea,
                                              "",
                                              "日志管理",
                                              "",
                                              this.getPri());
                this.columnManager.save(logColumn);
                
                        Column ologColumn = new Column(logColumn,
                                                       "",
                                                       "操作日志",
                                                       "column_221_img.gif",
                                                       "moduleinfor/logs/operatinglog/xtreeroot.jsp",
                                                       this.getPri());
                        this.columnManager.save(ologColumn);
                
                Column sysColumn = new Column(funcColumnsArea,
                                              "",
                                              "系统管理",
                                              "",
                                              this.getPri());
                this.columnManager.save(sysColumn);
                
                        Column orgColumn = new Column(sysColumn,
                                                      "",
                                                      "机构管理",
                                                      "new_column_4.gif",
                                                      "moduleinfor/sysframe/org/root.jsp",
                                                      this.getPri());
                        this.columnManager.save(orgColumn);
                        
                        Column roleColumn = new Column(sysColumn,
                                                       "",
                                                       "角色管理",
                                                       "column_140_img.gif",
                                                       "moduleinfor/sysframe/role/root.jsp",
                                                       this.getPri());
                        this.columnManager.save(roleColumn);
                        
                        Column postColumn = new Column(sysColumn,
                                                       "",
                                                       "岗位管理",
                                                       "column_13_img.gif",
                                                       "moduleinfor/sysframe/post/root.jsp",
                                                       this.getPri());
                        this.columnManager.save(postColumn);
                        
                        Column userColumn = new Column(sysColumn,
                                                       "",
                                                       "人员管理",
                                                       "column_245_img.gif",
                                                       "moduleinfor/sysframe/user/root.jsp",
                                                       this.getPri());
                        this.columnManager.save(userColumn);
                        
                        Column homeColumn = new Column(sysColumn,
                                                       "",
                                                       "主页管理",
                                                       "new_column_7.gif",
                                                       "moduleinfor/viewframe/home/root.jsp",
                                                       this.getPri());
                        this.columnManager.save(homeColumn);
                        
                        Column areaColumn = new Column(sysColumn,
                                                       "",
                                                       "分区管理",
                                                       "new_column_6.gif",
                                                       "moduleinfor/viewframe/area/root.jsp",
                                                       this.getPri());
                        this.columnManager.save(areaColumn);
                        
                        Column columnColumn = new Column(sysColumn,
                                                         "",
                                                         "栏目管理",
                                                         "GongWuSQ_01.gif",
                                                         "moduleinfor/viewframe/column/root.jsp",
                                                         this.getPri());
                        this.columnManager.save(columnColumn);
                        
                Column taskColumn = new Column(funcColumnsArea,
                                               "",
                                               "任务管理",
                                               "",
                                               this.getPri());
                this.columnManager.save(taskColumn);
                        Column taskRunningInfoColumn = new Column(taskColumn,
                                                                  "",
                                                                  "任务运行情况",
                                                                  "column_248_img.gif",
                                                                  "moduleinfor/system/taskrunninginfo/root.jsp",
                                                                  this.getPri());
                        this.columnManager.save(taskRunningInfoColumn);
                
                        
                
                // user角色 授权 个人事务
                userRole.addHome(defaultHome);
                userRole.addArea(funcColumnsArea);
                userRole.addColumn(moduleNameColumn);
                userRole.addColumn(modelNameColumn);
                userRole.addColumn(personColumn);
                userRole.addColumn(changepwColumn);
                this.roleManager.update(userRole);
                
                // 管理员授权 日志管理,系统管理                
                companyname_admin_post.addHome(defaultHome);
                companyname_admin_post.addArea(funcColumnsArea);
                
                companyname_admin_post.addColumn(basedataColumn);
                companyname_admin_post.addColumn(typeColumn);
                
                companyname_admin_post.addColumn(logColumn);
                companyname_admin_post.addColumn(ologColumn);
                
                companyname_admin_post.addColumn(sysColumn);
                companyname_admin_post.addColumn(orgColumn);
                companyname_admin_post.addColumn(roleColumn);
                companyname_admin_post.addColumn(postColumn);
                companyname_admin_post.addColumn(userColumn);
                companyname_admin_post.addColumn(homeColumn);
                companyname_admin_post.addColumn(areaColumn);
                companyname_admin_post.addColumn(columnColumn);
                
                companyname_admin_post.addColumn(taskColumn);
                companyname_admin_post.addColumn(taskRunningInfoColumn);
                
                this.postManager.update(companyname_admin_post);
                
                return admin; // 返回管理员
        }
        
        public void initBaseData(User admin)
        {
                Type root = new Type(null,TypeParams.ROOT,"  ",admin);
                root.setPri(this.getPri());
                this.typeManager.save(root);
                
                Type type_1 = new Type(root,ProjectNameTypeFinalParams.TYPE_1,"分类1",admin);
                type_1.setPri(this.getPri());
                this.typeManager.save(type_1);
                
        }
}
